import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BusinessMInfo, DeviceMAccess } from "@/api/backend/index";
import { useRegion } from "@/store/backend/SysM/region";
import qs from "qs";
import { getSum } from "@/utils/common";
import { useAccess } from "@/store/backend/DeviceM/access";

export const useOverView = defineStore(Names.overview, {
  state: () => {
    return {
      firstPageBusiness: [
        {
          addr: "青龙街道",
          code: null,
          createBy: null,
          createTime: "2023-01-13 14:58:46",
          creditCode: null,
          id: 7,
          isDel: 0,
          name: "江苏某家公司",
          params: {},
          region: "7,19,1,2",
          regionName: "三井街道",
          regionbo: 7,
          remark: null,
          searchValue: null,
          state: "0",
          tel: "15665665655",
          type: "某个行业",
          updateBy: null,
          updateTime: null,
          user: "测试",
        },
      ],
      firstPageDevices: [
        {
          addr: "江苏省常州市武进区礼新路81号",
          code: "TY7548",
          coordinate: null,
          createBy: null,
          createTime: null,
          describ: null,
          id: 20,
          institution: "3",
          model: "TY",
          name: "TY",
          openState: 1,
          params: {},
          power: 2000,
          productName: "净化器",
          recoveryDate: "2023-01-11",
          remark: null,
          searchValue: null,
          state: null,
          updateBy: null,
          updateTime: null,
          user: null,
          userPhone: null,
          validity: null,
        },
      ],
      devSum: 0, //设备总量
      newData: <any>[], //总览echarts数据
      main: false, //企业用户总览头部四个模块是否显示
    };
  },
  getters: {},
  actions: {
    //获取总览的设备统计 需要区域权限
    getEquipOver(data: any) {
      return new Promise((resolve, reject) => {
        const region = useRegion();
        _get(
          "/system/equipment/listByoverview?" +
            qs.stringify(data, { arrayFormat: "repeat" }),
          {},
          "",
          ""
        ).then(async (res: any) => {
          //0 停机 1 正常 2告警
          console.log("总览设备统计", res);
          if (res.rows.length > 0) {
            this.devSum = await getSum(res.rows); //设备总量
            res.rows.map((item: any) => {
              let obj = {
                value: item.value,
                name:
                  item.open_state === 0
                    ? "停机"
                    : item.open_state === 1
                    ? "正常"
                    : "告警",
              };
              this.newData.push(obj);
            });
            resolve(res.rows);
          } else {
            this.devSum = 0;
            resolve([
              { value: 0, open_state: 0 },
              { value: 0, open_state: 1 },
              { value: 0, open_state: 2 },
            ]);
          }
        });
      });
    },
    //获取最新接入设备
    getDevicesData(filter: any[]) {
      const access = useAccess();
      _get(
        DeviceMAccess + "/listBytree",
        {
          regions: filter + "",
          pageNum: 1,
          pageSize: 10,
          institution: access.companyid ? access.companyid : "" + "",
        },
        "",
        ""
      )
        .then((res: any) => {
          console.log(res);
          this.firstPageDevices = res.rows;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //获取最新接入企业
    getBusinessData(filter: any[]) {
      _get(
        BusinessMInfo + "/list",
        { regions: filter + "", pageNum: 1, pageSize: 10 },
        "",
        ""
      )
        .then((res: any) => {
          console.log(res);
          this.firstPageBusiness = res.rows;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
});
